<?php
namespace app\index\controller;

use app\index\model\Archives;
use app\index\model\ArchivesCategory;
use app\index\model\Interfile;
use app\index\model\User;
use think\Db;
use think\Request;

class File extends Common {
	protected $request;
	protected $ac_model;
	protected $a_model;
	protected $user_model;
	protected $interfile_model;
	protected $condition = array();
	public function __construct(Request $request = null) {
		parent::__construct($request);
		$this->request = $request;
		$this->ac_model = new ArchivesCategory();
		$this->a_model = new Archives();
		$this->user_model = new User();
		$this->interfile_model = new Interfile();
		$this->condition['cate_type'] = 1;
		/* //不是管理员的情况下  只看自己和 自己下面部门的文件
			         if($_SESSION['think']['username'][0]['user_name'] != 'admin'){
			             $condition['create_user_id'] =$_SESSION['think']['username'][0]['user_id'];
		*/
		$cond = $this->getRoleByUser();
		if ($cond != 'all') {
			$this->condition['create_user_id'] = array('in', $cond);
		}
	}

	// 归档
	public function index() {
		$this->getAuth();

		$user_ids = $this->getRoleByUser();
		$user_ids = explode(',', $user_ids);
		$params = $this->request->param();
		$condition = array();
		if (!empty($params['type'])) {
			if ($params['type'] == 1) {
				$condition['interfile_status'] = array('IN', array(1, 2));
			} else {
				$condition['interfile_status'] = $params['type'];
			}
		}
		if (in_array('all', $user_ids)) {
			$in_data = $this->interfile_model->getListByConditionPage($condition, 'interfile_id desc');
		} else {
			$condition['create_user_id'] = array('IN', $user_ids);
			$in_data = $this->interfile_model->getListByConditionPage($condition, 'interfile_id desc');
		}
		if ($in_data) {
			foreach ($in_data as $key => $val) {

				//$arc_data = $this->a_model->getDataByCondition(array('arc_id' => $val['arc_id']));
				$arc_data = $this->a_model->where(array('arc_id' => $val['arc_id']))->find();

				if ($arc_data == null) {
					dump('数据有误请在档案表删除' . $val['arc_id']);
					die;
				}
				$dg_path = $this->getFilePathUrl1($val['interfile_cate_id'], 2);
				if ($arc_data) {
					$in_data[$key]['arc_name'] = $arc_data['arc_name'];
					$in_data[$key]['arc_type'] = $arc_data['arc_type'];
					$in_data[$key]['arc_file_size'] = $arc_data['arc_file_size'];
					$in_data[$key]['path'] = $dg_path['path'];
					$in_data[$key]['audit_status'] = $arc_data['audit_status'];
					$in_data[$key]['delete'] = $arc_data['is_delete'];
				}
			}
			$page = $in_data->render();
		}

		//$q_num = $this->interfile_model->getCountByCondition(array());
		if (in_array('all', $user_ids)) {
			$q_num = $this->interfile_model->getCountByCondition(array());
			$d_num = $this->interfile_model->getCountByCondition(array('interfile_status' => array('IN', array(1, 2))));
			$y_num = $this->interfile_model->getCountByCondition(array('interfile_status' => 3));
		} else {
			$q_num = count($in_data);

			$d_num = $this->interfile_model->getCountByCondition(array('interfile_status' => array('IN', array(1, 2)), 'create_user_id' => array('IN', $user_ids)));
			$y_num = $this->interfile_model->getCountByCondition(array('interfile_status' => 3, 'create_user_id' => array('IN', $user_ids)));
		}

		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		$prev = Db('_user_log')
			->where('user_id', $uid)
			->order('create_time desc')
			->limit(0, 1)
			->field('log_desc')
			->find();
		$prevdata = $prev['log_desc'];

		if ($prevdata !== '归档') {
			//记录用户操作
			$data = [
				'user_id' => $uid,
				'log_action' => 'File/index',
				'log_desc' => '归档', //默认空
				'create_time' => $time,
				'is_delete' => 0, //默认0
			];
			$res = db('_user_log')
				->insert($data);
		}
		return view('index@File/index', ['in_data' => $in_data, 'q_num' => $q_num, 'd_num' => $d_num, 'y_num' => $y_num, 'page' => $page]);
	}

	//添加文档到归档库里面
	public function addFile() {
		$this->getAuth();
		$map['cate_type'] = 1;
		if (!empty($this->condition['create_user_id'])) {
			$map['create_org_id'] = $this->getOrgIdByUid($_SESSION['think']['username'][0]['user_id']);
		}
		$ac_data = $this->ac_model->getListByCondition($map);

		if ($ac_data) {
			$cate_tree = recursionCate($ac_data, 'cate_id', 0, 'cate_parent_id');
			$tree = createCateTree($cate_tree, 'cate_id', 'cate_parent_id', 'cate_name');
		} else {
			$tree = '';
		}
		$user_ids = $this->getRoleByUser();
		$where['cate_type'] = 2;
		/*if($_SESSION['think']['username'][0]['user_name'] != 'admin'){
			            $where['create_user_id'] = $this->condition['create_user_id'];
		*/
		$ac_data1 = $this->ac_model->getListByCondition($where);
		if ($ac_data1) {
			$cate_tree1 = recursionCate($ac_data1, 'cate_id', 0, 'cate_parent_id');
			$tree1 = createCateTree($cate_tree1, 'cate_id', 'cate_parent_id', 'cate_name');
		} else {
			$tree1 = '';
		}
		if ($this->request->instance()->isPost()) {
			$arc_id = $this->request->post('arc_id');
			$interfile_cate_id = $this->request->post('interfile_cate_id');
			$interfile_desc = $this->request->post('interfile_desc');
			if (empty($arc_id)) {
				jsonOut(0, '预归档文件不能为空');
			}
			if (empty($interfile_cate_id)) {
				jsonOut(0, '归档目录错误');
			}
			$arc_data = $this->a_model->getDataByCondition(array('arc_id' => $arc_id));
			if (empty($arc_data)) {
				jsonOut(0, '归档文件未找到');
			}
			$p_data = $this->ac_model->getDataByCondition(array('cate_id' => $interfile_cate_id));
			if (empty($p_data)) {
				jsonOut(0, '归档目录错误');
			}
			$save_data = array(
				'arc_id' => $arc_data['arc_id'],
				'cate_id' => $arc_data['category_id'],
				'interfile_cate_id' => $interfile_cate_id,
				'interfile_desc' => $interfile_desc,
				'interfile_status' => 1,
				'create_user_id' => $this->admin_session['id'],
				'create_time' => date('Y-m-d H:i:s'),
				'update_user_id' => $this->admin_session['id'],
				'update_time' => date('Y-m-d H:i:s'),
				'interfile_audit_comments' => '提交审核中',
			);
			if ($this->interfile_model->insert($save_data)) {
				$id = $this->interfile_model->getLastInsID();
				$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', '创建');
				//修改文档状态
				$this->a_model->updateByCondition(array('arc_id' => $arc_id), array('audit_status' => 1));
				jsonOut(1, '归档申请成功');
			} else {
				jsonOut(0, '归档申请失败');
			}
		}

		return view('index@File/addFile', ['tree' => $tree, 'tree1' => $tree1]);
	}
	//根据分类ID
	public function getArcByCateId() {
		$id = $this->request->post('id');
		if ($_SESSION['think']['username'][0]['user_name'] != 'admin') {
			//日了狗  又要改
			// if ($_SESSION['think']['username'][0]['level'] == '1') {
			$uidnums = $this->condition['create_user_id'];
			$data = $this->a_model->getListByCondition(array('category_id' => $id, 'audit_status' => array('IN', array(0, 3)), 'create_user_id' => $uidnums));
			// } else {
			// 	$data = $this->a_model->getListByCondition(array('category_id' => $id, 'audit_status' => array('IN', array(0, 3)), 'create_user_id' => $_SESSION['think']['username'][0]['user_id']));
			// }

		} else {
			$data = $this->a_model->getListByCondition(array('category_id' => $id, 'audit_status' => array('IN', array(0, 3))));
		}
		//dump($data);die;
		if ($data) {
			$html = '';
			foreach ($data as $key => $val) {
				$update_username = $this->user_model->getDataByCondition(array('user_id' => $val['update_user_id']));
				$html .= '<tr>' .
				'<td class="highlight">' .
				'<div class="success"></div>' .
				'<a href="javascript:;">' . $val['arc_name'] . '.' . $val['arc_type'] . '</a>' .
				'</td>' .
				'<td>' .
				'<a href="javascript:;">' . $update_username['user_name'] . '</a>' .
				'</td>' .
				'<td>' .
				'<a href="javascript:;">' . count_size($val['arc_file_size']) . '</a>' .
					'</td>' .
					'<td>' .
					'<a href="javascript:;" class="btn default btn-xs purple editBtn" data-name="' . $val['arc_name'] . '.' . $val['arc_type'] . '" data-type="1" data-id="' . $val['arc_id'] . '">' .
					'<i class="fa fa-edit"></i> 选择' .
					'</a>' .
					'</td>' .
					'</tr>';
			}
			jsonOut(1, '查询成功', $html);
		} else {
			jsonOut(0, '暂无数据', '');
		}
	}
	//根据ID 取对应的数据
	public function getPathById() {
		$id = $this->request->post('id');
		if (empty($id)) {
			jsonOut(0, '参数错误');
		}
		$path = $this->getFilePathUrl1($id, 2);
		if ($path['code'] != 1) {
			jsonOut(0, '暂无数据');
		} else {
			jsonOut(1, '查询成功', $path['path']);
		}

	}
	//审核操作
	public function auditFile() {

		$this->getAuth();

		$id = $this->request->get('id');
		if ($this->request->instance()->isPost()) {
			$id = $this->request->post('id');
			$interfile_audit_comments = $this->request->post('interfile_audit_comments');
			$fq = $this->request->post('fq');
			$dh = $this->request->post('dh');

			if (empty($id)) {
				jsonOut(0, '参数错误');
			}
			$data = $this->interfile_model->getDataByCondition(array('interfile_id' => $id));
			if (empty($data)) {
				jsonOut(0, '暂无数据');
			}
			if (!empty($fq)) {
				if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), array('is_delete' => 1, 'interfile_status' => 1, 'update_user_id' => $this->admin_session['id']))) {
					//修改原始文件状态
					if ($this->a_model->updateByCondition(array('arc_id' => $data['arc_id']), array('audit_status' => 0, 'update_user_id' => $this->admin_session['id']))) {
						$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', '废弃归档申请');
						$this->tableBak('archives', 'archives_version', 'arc_id', 'original_id', $id, 'backup_reasons', '废弃归档申请，改变文档状态');
						jsonOut(1, '废弃申请成功');
					} else {
						jsonOut(0, '原文件状态更新失败');
					}
				} else {
					jsonOut(0, '废弃申请失败');
				}
			}
			if (!empty($dh)) {
				if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), array('interfile_status' => 1, 'update_user_id' => $this->admin_session['id']))) {
					$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', '打回归档申请');
					jsonOut(1, '打回申请成功');
				} else {
					jsonOut(0, '打回申请失败');
				}
			}

			if ($data['interfile_status'] == 1) {
				$show = '补充意见不能为空';
				$interfile_status = 2;
				$remark = '补充意见审核';
			} else {
				$show = '审核评论意见不能为空';
				$interfile_status = 3;
				$remark = '审核通过';
			}
			if (empty($interfile_audit_comments)) {
				jsonOut(0, $show);
			}
			$save_data = array(
				'interfile_audit_comments' => $interfile_audit_comments,
				'interfile_status' => $interfile_status,
				'update_user_id' => $this->admin_session['id'],
			);
			if ($data['interfile_status'] == 2) {
				//审核通过了 移动目标文件到新的目录
				// $arc_data = $this->a_model->getDataByCondition(array('arc_id' => $data['arc_id']));
				// $path = $this->getFilePath($data['cate_id'], 1, $data['arc_id']);

				// $new_path = $this->getFilePath($data['interfile_cate_id'], 2);

				/*if(is_file(iconv("UTF-8", "GBK",$new_path['path'].'/'.$arc_data['arc_name'].'.'.$arc_data['arc_type']))){
					                    jsonOut(0,'文件已存在');
				*/
				// if (is_file($new_path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type'])) {
				// 	jsonOut(0, '文件已存在');
				// }
				//1
				//$cp = copy(iconv("UTF-8", "GBK",$path['path'].'/'.$arc_data['arc_name'].'.'.$arc_data['arc_type']),iconv("UTF-8", "GBK",$new_path['path'].'/'.$arc_data['arc_name'].'.'.$arc_data['arc_type']));
				// $cp = copy($path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type'], $new_path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type']);
				// var_dump($cp);die;
				// if ($cp) {
					if ($this->a_model->updateByCondition(array('arc_id' => $data['arc_id']), array('category_id' => $data['interfile_cate_id'], 'audit_status' => 2, 'update_user_id' => $this->admin_session['id']))) {
						//@unlink(iconv("UTF-8", "GBK", $path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type']));
						// @unlink($path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type']);
						$this->tableBak('archives', 'archives_version', 'arc_id', 'original_id', $id, 'backup_reasons', '归档');
						if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), $save_data)) {
							$this->a_model->updateByCondition(array('arc_id' => $id), array('update_time' => date('Y-m-d H:i:s', time())));
							$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', $remark);
							jsonOut(1, '归档成功');
						} else {
							jsonOut(0, '归档失败');
						}
					} else {
						jsonOut(0, '文件状态更新失败');
					}
				// } else {
				// 	jsonOut(0, '文件移动到归档目录失败');
				// }
			} else {
				if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), $save_data)) {
					$this->a_model->updateByCondition(array('arc_id' => $data['arc_id']), array('update_user_id' => $_SESSION['think']['username'][0]['user_id']));
					$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', $remark);
					jsonOut(1, '审核成功');
				} else {
					jsonOut(0, '审核失败');
				}
			}
		}
		if (empty($id)) {
			jsonOut(0, '参数错误');
		}
		$data = $this->interfile_model->getDataByCondition(array('interfile_id' => $id));
		if (empty($data)) {
			jsonOut(0, '暂无数据');
		}
		$arc_data = $this->a_model->getDataByCondition(array('arc_id' => $data['arc_id']));
		//根据arc_data中更新者id获取name
		$update_username = $this->user_model->getDataByCondition(array('user_id' => $arc_data['update_user_id']));
		$dg_path = $this->getFilePathUrl1($data['interfile_cate_id'], 2);
		if ($arc_data) {
			$data['arc_name'] = $arc_data['arc_name'];
			$data['arc_type'] = $arc_data['arc_type'];
			$data['arc_file_size'] = $arc_data['arc_file_size'];
			$data['path'] = $dg_path['path'];
			$data['update_username'] = $update_username['user_name'];
		}
		return view('index@File/auditFile', ['data' => $data]);
	}

	//所有人都能看详细
	public function fqcz() {

		$id = $this->request->get('id');
		if ($this->request->instance()->isPost()) {
			$id = $this->request->post('id');
			$interfile_audit_comments = $this->request->post('interfile_audit_comments');
			$fq = $this->request->post('fq');
			$dh = $this->request->post('dh');

			if (empty($id)) {
				jsonOut(0, '参数错误');
			}
			$data = $this->interfile_model->getDataByCondition(array('interfile_id' => $id));
			if (empty($data)) {
				jsonOut(0, '暂无数据');
			}
			if (!empty($fq)) {
				if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), array('is_delete' => 1, 'interfile_status' => 1, 'update_user_id' => $this->admin_session['id']))) {
					//修改原始文件状态
					if ($this->a_model->updateByCondition(array('arc_id' => $data['arc_id']), array('audit_status' => 0, 'update_user_id' => $this->admin_session['id']))) {
						$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', '废弃归档申请');
						$this->tableBak('archives', 'archives_version', 'arc_id', 'original_id', $id, 'backup_reasons', '废弃归档申请，改变文档状态');
						jsonOut(1, '废弃申请成功');
					} else {
						jsonOut(0, '原文件状态更新失败');
					}
				} else {
					jsonOut(0, '废弃申请失败');
				}
			}
			if (!empty($dh)) {
				if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), array('interfile_status' => 1, 'update_user_id' => $this->admin_session['id']))) {
					$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', '打回归档申请');
					jsonOut(1, '打回申请成功');
				} else {
					jsonOut(0, '打回申请失败');
				}
			}

			if ($data['interfile_status'] == 1) {
				$show = '补充意见不能为空';
				$interfile_status = 2;
				$remark = '补充意见审核';
			} else {
				$show = '审核评论意见不能为空';
				$interfile_status = 3;
				$remark = '审核通过';
			}
			if (empty($interfile_audit_comments)) {
				jsonOut(0, $show);
			}
			$save_data = array(
				'interfile_audit_comments' => $interfile_audit_comments,
				'interfile_status' => $interfile_status,
				'update_user_id' => $this->admin_session['id'],
			);
			if ($data['interfile_status'] == 2) {
				//审核通过了 移动目标文件到新的目录
				// $arc_data = $this->a_model->getDataByCondition(array('arc_id' => $data['arc_id']));
				// $path = $this->getFilePath($data['cate_id'], 1, $data['arc_id']);

				// $new_path = $this->getFilePath($data['interfile_cate_id'], 2);

				/*if(is_file(iconv("UTF-8", "GBK",$new_path['path'].'/'.$arc_data['arc_name'].'.'.$arc_data['arc_type']))){
					                    jsonOut(0,'文件已存在');
				*/
				// if (is_file($new_path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type'])) {
				// 	jsonOut(0, '文件已存在');
				// }
				//1
				//$cp = copy(iconv("UTF-8", "GBK",$path['path'].'/'.$arc_data['arc_name'].'.'.$arc_data['arc_type']),iconv("UTF-8", "GBK",$new_path['path'].'/'.$arc_data['arc_name'].'.'.$arc_data['arc_type']));
				// $cp = copy($path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type'], $new_path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type']);
				// var_dump($cp);die;
				// if ($cp) {
					if ($this->a_model->updateByCondition(array('arc_id' => $data['arc_id']), array('category_id' => $data['interfile_cate_id'], 'audit_status' => 2, 'update_user_id' => $this->admin_session['id']))) {
						//@unlink(iconv("UTF-8", "GBK", $path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type']));
						// @unlink($path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type']);
						$this->tableBak('archives', 'archives_version', 'arc_id', 'original_id', $id, 'backup_reasons', '归档');
						if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), $save_data)) {
							$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', $remark);
							jsonOut(1, '归档成功');
						} else {
							jsonOut(0, '归档失败');
						}
					} else {
						jsonOut(0, '文件状态更新失败');
					}
				// } else {
				// 	jsonOut(0, '文件移动到归档目录失败');
				// }
			} else {
				if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), $save_data)) {
					$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', $remark);
					jsonOut(1, '审核成功');
				} else {
					jsonOut(0, '审核失败');
				}
			}
		}
		if (empty($id)) {
			jsonOut(0, '参数错误');
		}
		//根据id找到cate_id
		$data = $this->interfile_model->getDataByCondition(array('interfile_id' => $id));
		$arc_id = $data['arc_id'];
		$cate_id = $data['cate_id'];
		$ndata = Db::table('cn_interfile_version')
			->where('cate_id', $cate_id)
			->where('arc_id', $arc_id)
			->select();
		for ($i = 0; $i < count($ndata); $i++) {
			$arr[$i] = Db::table('cn_user')
				->where('user_id', $ndata[$i]['update_user_id'])
				->field('user_name')
				->find();
			$ndata[$i]['user_name'] = $arr[$i]['user_name'];
		}

		if (empty($data)) {
			jsonOut(0, '暂无数据');
		}
		$arc_data = $this->a_model->getDataByCondition(array('arc_id' => $data['arc_id']));
		//根据arc_data中更新者id获取name
		$update_username = $this->user_model->getDataByCondition(array('user_id' => $arc_data['update_user_id']));
		$dg_path = $this->getFilePathUrl1($data['interfile_cate_id'], 2);
		if ($arc_data) {
			$data['arc_name'] = $arc_data['arc_name'];
			$data['arc_type'] = $arc_data['arc_type'];
			$data['arc_file_size'] = $arc_data['arc_file_size'];
			$data['path'] = $dg_path['path'];
			$data['update_username'] = $update_username['user_name'];
		}
		return view('index@File/auditFile', ['data' => $data, 'ndata' => $ndata]);
	}

	public function dhsq() {

		$id = $this->request->get('id');
		if ($this->request->instance()->isPost()) {
			$id = $this->request->post('id');
			$interfile_audit_comments = $this->request->post('interfile_audit_comments');
			$fq = $this->request->post('fq');
			$dh = $this->request->post('dh');

			if (empty($id)) {
				jsonOut(0, '参数错误');
			}
			$data = $this->interfile_model->getDataByCondition(array('interfile_id' => $id));
			if (empty($data)) {
				jsonOut(0, '暂无数据');
			}
			if (!empty($fq)) {
				if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), array('is_delete' => 1, 'interfile_status' => 1, 'update_user_id' => $this->admin_session['id']))) {
					//修改原始文件状态
					if ($this->a_model->updateByCondition(array('arc_id' => $data['arc_id']), array('audit_status' => 0, 'update_user_id' => $this->admin_session['id']))) {
						$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', '废弃归档申请');
						$this->tableBak('archives', 'archives_version', 'arc_id', 'original_id', $id, 'backup_reasons', '废弃归档申请，改变文档状态');
						jsonOut(1, '废弃申请成功');
					} else {
						jsonOut(0, '原文件状态更新失败');
					}
				} else {
					jsonOut(0, '废弃申请失败');
				}
			}
			if (!empty($dh)) {
				if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), array('interfile_status' => 1, 'update_user_id' => $this->admin_session['id']))) {
					$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', '打回归档申请');
					jsonOut(1, '打回申请成功');
				} else {
					jsonOut(0, '打回申请失败');
				}
			}

			if ($data['interfile_status'] == 1) {
				$show = '补充意见不能为空';
				$interfile_status = 2;
				$remark = '补充意见审核';
			} else {
				$show = '审核评论意见不能为空';
				$interfile_status = 3;
				$remark = '审核通过';
			}
			if (empty($interfile_audit_comments)) {
				jsonOut(0, $show);
			}
			$save_data = array(
				'interfile_audit_comments' => $interfile_audit_comments,
				'interfile_status' => $interfile_status,
				'update_user_id' => $this->admin_session['id'],
			);
			if ($data['interfile_status'] == 2) {
				//审核通过了 移动目标文件到新的目录
				// $arc_data = $this->a_model->getDataByCondition(array('arc_id' => $data['arc_id']));
				// $path = $this->getFilePath($data['cate_id'], 1, $data['arc_id']);

				// $new_path = $this->getFilePath($data['interfile_cate_id'], 2);

				/*if(is_file(iconv("UTF-8", "GBK",$new_path['path'].'/'.$arc_data['arc_name'].'.'.$arc_data['arc_type']))){
					                    jsonOut(0,'文件已存在');
				*/
				// if (is_file($new_path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type'])) {
				// 	jsonOut(0, '文件已存在');
				// }
				//1
				//$cp = copy(iconv("UTF-8", "GBK",$path['path'].'/'.$arc_data['arc_name'].'.'.$arc_data['arc_type']),iconv("UTF-8", "GBK",$new_path['path'].'/'.$arc_data['arc_name'].'.'.$arc_data['arc_type']));
				// $cp = copy($path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type'], $new_path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type']);
				// var_dump($cp);die;
				// if ($cp) {
					if ($this->a_model->updateByCondition(array('arc_id' => $data['arc_id']), array('category_id' => $data['interfile_cate_id'], 'audit_status' => 2, 'update_user_id' => $this->admin_session['id']))) {
						//@unlink(iconv("UTF-8", "GBK", $path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type']));
						// @unlink($path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type']);
						$this->tableBak('archives', 'archives_version', 'arc_id', 'original_id', $id, 'backup_reasons', '归档');
						if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), $save_data)) {
							$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', $remark);
							jsonOut(1, '归档成功');
						} else {
							jsonOut(0, '归档失败');
						}
					} else {
						jsonOut(0, '文件状态更新失败');
					}
				// } else {
				// 	jsonOut(0, '文件移动到归档目录失败');
				// }
			} else {
				if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), $save_data)) {

					$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', $remark);
					jsonOut(1, '审核成功');
				} else {
					jsonOut(0, '审核失败');
				}
			}
		}
		if (empty($id)) {
			jsonOut(0, '参数错误');
		}
		$data = $this->interfile_model->getDataByCondition(array('interfile_id' => $id));
		if (empty($data)) {
			jsonOut(0, '暂无数据');
		}
		$arc_data = $this->a_model->getDataByCondition(array('arc_id' => $data['arc_id']));
		//根据arc_data中更新者id获取name
		$update_username = $this->user_model->getDataByCondition(array('user_id' => $arc_data['update_user_id']));
		$dg_path = $this->getFilePathUrl1($data['interfile_cate_id'], 2);
		if ($arc_data) {
			$data['arc_name'] = $arc_data['arc_name'];
			$data['arc_type'] = $arc_data['arc_type'];
			$data['arc_file_size'] = $arc_data['arc_file_size'];
			$data['path'] = $dg_path['path'];
			$data['update_username'] = $update_username['user_name'];
		}
		return view('index@File/auditFile', ['data' => $data]);
	}

	// 第二次审批
	public function secondsh() {
		$this->getAuth();
		$id = $this->request->get('id');
		if ($this->request->instance()->isPost()) {
			$id = $this->request->post('id');
			$interfile_audit_comments = $this->request->post('interfile_audit_comments');
			$fq = $this->request->post('fq');
			$dh = $this->request->post('dh');

			if (empty($id)) {
				jsonOut(0, '参数错误');
			}
			$data = $this->interfile_model->getDataByCondition(array('interfile_id' => $id));
			if (empty($data)) {
				jsonOut(0, '暂无数据');
			}
			if (!empty($fq)) {
				if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), array('is_delete' => 1, 'interfile_status' => 1, 'update_user_id' => $this->admin_session['id']))) {
					//修改原始文件状态
					if ($this->a_model->updateByCondition(array('arc_id' => $data['arc_id']), array('audit_status' => 0, 'update_user_id' => $this->admin_session['id']))) {
						$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', '废弃归档申请');
						$this->tableBak('archives', 'archives_version', 'arc_id', 'original_id', $id, 'backup_reasons', '废弃归档申请，改变文档状态');
						jsonOut(1, '废弃申请成功');
					} else {
						jsonOut(0, '原文件状态更新失败');
					}
				} else {
					jsonOut(0, '废弃申请失败');
				}
			}
			if (!empty($dh)) {
				if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), array('interfile_status' => 1, 'update_user_id' => $this->admin_session['id']))) {
					$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', '打回归档申请');
					jsonOut(1, '打回申请成功');
				} else {
					jsonOut(0, '打回申请失败');
				}
			}

			if ($data['interfile_status'] == 1) {
				$show = '补充意见不能为空';
				$interfile_status = 2;
				$remark = '补充意见审核';
			} else {
				$show = '审核评论意见不能为空';
				$interfile_status = 3;
				$remark = '审核通过';
			}
			if (empty($interfile_audit_comments)) {
				jsonOut(0, $show);
			}
			$save_data = array(
				'interfile_audit_comments' => $interfile_audit_comments,
				'interfile_status' => $interfile_status,
				'update_user_id' => $this->admin_session['id'],
				'update_time' => date('Y-m-d H:i:s'),
			);
			if ($data['interfile_status'] == 2) {
				//审核通过了 移动目标文件到新的目录
				// $arc_data = $this->a_model->getDataByCondition(array('arc_id' => $data['arc_id']));
				// $path = $this->getFilePath($data['cate_id'], 1, $data['arc_id']);

				// $new_path = $this->getFilePath($data['interfile_cate_id'], 2);

				/*if(is_file(iconv("UTF-8", "GBK",$new_path['path'].'/'.$arc_data['arc_name'].'.'.$arc_data['arc_type']))){
					                    jsonOut(0,'文件已存在');
				*/
				// if (is_file($new_path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type'])) {
				// 	jsonOut(0, '文件已存在');
				// }
				//1
				//$cp = copy(iconv("UTF-8", "GBK",$path['path'].'/'.$arc_data['arc_name'].'.'.$arc_data['arc_type']),iconv("UTF-8", "GBK",$new_path['path'].'/'.$arc_data['arc_name'].'.'.$arc_data['arc_type']));
				// $cp = copy($path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type'], $new_path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type']);
				// var_dump($cp);die;
				// if ($cp) {
					if ($this->a_model->updateByCondition(array('arc_id' => $data['arc_id']), array('category_id' => $data['interfile_cate_id'], 'audit_status' => 2, 'update_user_id' => $this->admin_session['id']))) {
						//@unlink(iconv("UTF-8", "GBK", $path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type']));
						// @unlink($path['path'] . '/' . $arc_data['arc_name'] . '.' . $arc_data['arc_type']);
						$this->tableBak('archives', 'archives_version', 'arc_id', 'original_id', $id, 'backup_reasons', '归档');
						if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), $save_data)) {
							$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', $remark);
							$this->a_model->updateByCondition(array('arc_id' => $id), array('update_time' => date('Y-m-d H:i:s', time())));
							jsonOut(1, '归档成功');
						} else {
							jsonOut(0, '归档失败');
						}
					} else {
						jsonOut(0, '文件状态更新失败');
					}
				// } else {
				// 	jsonOut(0, '文件移动到归档目录失败');
				// }
			} else {
				if ($this->interfile_model->updateByCondition(array('interfile_id' => $id), $save_data)) {
					$this->tableBak('interfile', 'interfile_version', 'interfile_id', 'original_id', $id, 'backup_reasons', $remark);
					jsonOut(1, '审核成功');
				} else {
					jsonOut(0, '审核失败');
				}
			}
		}
		if (empty($id)) {
			jsonOut(0, '参数错误');
		}
		$data = $this->interfile_model->getDataByCondition(array('interfile_id' => $id));
		if (empty($data)) {
			jsonOut(0, '暂无数据');
		}
		$arc_data = $this->a_model->getDataByCondition(array('arc_id' => $data['arc_id']));
		//根据arc_data中更新者id获取name
		$update_username = $this->user_model->getDataByCondition(array('user_id' => $arc_data['update_user_id']));
		$dg_path = $this->getFilePathUrl1($data['interfile_cate_id'], 2);
		if ($arc_data) {
			$data['arc_name'] = $arc_data['arc_name'];
			$data['arc_type'] = $arc_data['arc_type'];
			$data['arc_file_size'] = $arc_data['arc_file_size'];
			$data['path'] = $dg_path['path'];
			$data['update_username'] = $update_username['user_name'];
		}
		return view('index@File/auditFile', ['data' => $data]);
	}

}
